package com.example.demo_xupdate;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.xuexiang.xupdate.XUpdate;
import com.xuexiang.xupdate.entity.UpdateEntity;
import com.xuexiang.xupdate.listener.IUpdateParseCallback;
import com.xuexiang.xupdate.proxy.IUpdateParser;
import com.xuexiang.xutil.net.JsonUtil;
import com.xuexiang.xutil.resource.ResUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class MainActivity extends AppCompatActivity {

    private String mUpdateUrl = "https://gitee.com/xuexiangjys/XUpdate/raw/master/jsonapi/update_test.json";
    private String mUpdateUrl3 = "https://gitee.com/xuexiangjys/XUpdate/raw/master/jsonapi/update_custom.json";
    private String mUpdateUrl4 = "https://gitee.com/Robotyangliqiang/demo_-xupdate/raw/master/app/src/main/res/assets/apkInfo.txt";
    private String mUpdateUrl5 = "";
    Button btnUpdate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnUpdate = findViewById(R.id.btn_update);
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                XUpdate.newBuild(getContext())
                        .updateUrl(mUpdateUrl4)
                        .updateParser(new CustomUpdateParser())
                        .supportBackgroundUpdate(true)
                        .update();
            }
        });
    }

        class CustomUpdateParser implements IUpdateParser {
            @Override
            public UpdateEntity parseJson(String json) throws Exception {
                ApkInfo result = JsonUtil.fromJson(json, ApkInfo.class);
                if (result != null) {
                    return new UpdateEntity()
                            .setHasUpdate(true)
                            .setIsIgnorable(false)
                            .setVersionCode(result.getLatestVersionCode())
                            .setVersionName(result.getLatestVersionName())
                            .setUpdateContent(result.getLatestVersionDesc())
                            .setDownloadUrl(result.getDownloadUrl())
                            .setSize(result.getApkSize())
                            .setIsAutoInstall(true)
                            .setShowNotification(true);

                }
                return null;
            }

            @Override
            public void parseJson(String json, IUpdateParseCallback callback) throws Exception {

            }

            @Override
            public boolean isAsyncParser() {
                return false;
            }


        }

    public Context getContext(){
        return this;
    }
    public String getJson(String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = MainActivity.this.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
                Log.d("AAA", line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


}